(function($){
	function create(tagName, className){
	    var e = document.createElement(tagName);
	    if(!!className){
	        e.className = className;
	    }
	    return e;
	}
	function getProcessGraph(nodes, currentNodeId, __basePath, auditorMap){
	    nodes.sort(function(a, b){
	        return a.serino > b.serino? 1: a.serino < b.serino? -1: 0;
	    })
	    var container = create("div", "process-graph-container");

	    //å¶ä»èç¹
	    for(var i = 0; i < nodes.length; i++){
	    	if(i != 0){
	            container.appendChild(getDownPointDom(__basePath));
	    	}
	        var point = getProcessPointDom(nodes[i], nodes[i].type);
	        if(nodes[i].id === currentNodeId){
	            point.className += " active-point";
	        }
	        container.appendChild(point);
	    }

	    return container;
	}

	function getDownPointDom(__basePath){
	    var down = create("img", "process-down-pointer");
	    down.src = __basePath + "img/icon/down-pointer.png";
	    return down;
	}
	function getProcessPointDom(node, type){
	    var className = "process-point";
	    if(type === 0){
	        className += " start-point";
	    }else if(type === 1){
	        className += " task-point";
	    }else if(type === 2){
	        className += " end-point";
	    }else {
	        throw "unknown point type";
	    }
	    var div  = create("div", className);
	    div.innerText = node.name;
	    return div;
	}
	window.ProcessHelper = function(basePath){
	    var __basePath = basePath || "";
	    var __hasTable = false;
	    var auditorCheckName = "auditor";
	    var tableClassName = "table-auditor-list";
	    var checkboxClassName = "auditor-checkbox";

	    function getCheckboxHtml(className, name, value, attr, checkable){
	        return '<div class="pretty success smooth">'
	                  + '<input class="'+(className || "")+'" value="'+(value || "")+'" type="checkbox" name="'+(name || "")+'" '+attr+' '+(checkable? '': 'onclick="return false;"')+'>'
	                  + '<label><i class="mdi mdi-check"></i></label>'
	               + '</div>'
	    }

	    function getSelectedAuditor(){
	       var arr = [];
	       var map = {};
	       $("." + tableClassName).find("input[name='"+ auditorCheckName +"']:checked").each(function(){
	    	   if(map[this.value]){
	    		   return;
	    	   }
	    	   map[this.value] = true;
	           arr.push(this.value);
	       })
	       return arr;
	    }
	    function getPreApprovalTable(node, auditorMap){
	        if(!(node || auditorMap)){
	            return;
	        }
	        var selfselectapp = node.selfselectapp == 1;
	        var container = create("div", tableClassName);
	        for(var k in auditorMap){
	            var auditor = auditorMap[k];

	            var itemDiv = create("div");
	            itemDiv.innerHTML = getCheckboxHtml(checkboxClassName, auditorCheckName, auditor.id, 'checked="checked"', selfselectapp);

	            var labelSpan = create("span");
	            labelSpan.innerText = auditor.nickname;

	            itemDiv.appendChild(labelSpan);
	            container.appendChild(itemDiv);
	            __hasTable = true;
	        }
	        return container;
	    }

	    this.hasTable = function(){
	        return __hasTable;
	    };
	    
	    this.getPreApprovalTable = getPreApprovalTable;
	    this.getSelectedAuditor = getSelectedAuditor;
	    this.getProcessGraph = function(nodes, currentNodeId, auditorMap){
	    	return getProcessGraph(nodes, currentNodeId, __basePath, auditorMap);
	    };
	}
	$.fn.processGraph = function(options){
		var defaults = {
				uri: "manage/getCurApprovalInfo",
		};
		var opts = $.extend({}, defaults, options);
		var BASE_PATH = opts["basePath"];
		var URI = opts["uri"];
		var __that = this;
		function init(){
	    	$.post(BASE_PATH + URI, {instance_id: opts["instance_id"]}, function(data){
	    		var nodes = data.nodes;
	    		var cur = data.cur;
	    		var auditorMap = data.auditorMap;
	    		if(opts["useData"] instanceof Function){
	    			opts["useData"](nodes, cur, auditorMap);
	    		}
	    		__that.append(getProcessGraph(nodes, cur.id, opts["basePath"], auditorMap));
	        })
		}
		init();
		return this;
	}
})(jQuery)
